<template>
<el-card  style="width: 500px ;margin: auto">
  <h1>注册/登录</h1>
  <el-form :model="loginReq" status-icon :rules="rules" ref="ruleForm" label-width="100px"   class="demo-ruleForm">
    <el-form-item label="手机号" prop="phone">
      <el-input v-model.number="loginReq.phone"></el-input>
    </el-form-item>
    <el-form-item label="验证码" prop="code">
      <div style="float: left">
      <el-input  v-model="loginReq.code" autocomplete="off"  style="width: 200px"></el-input>
      <el-button @click="sendMsg" type="primary" :disabled="canClick">{{ content }}</el-button>
      </div>
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
    </el-form-item>
  </el-form>
</el-card>
</template>

<script>
export default {
  name: "ulogin",
  data() {
    return {
      loginReq: {
        phone:'',
        code: '',
      },
      content: '发送短信',
      totalTime: 60,
      canClick: false,
      rules: {
        phone: [
          {required: true, message: '请输入手机号', trigger: 'blur'},
        ],
        code: [
          {required: true, message: '请输入验证码', trigger: 'blur'},
        ],
      }
    };
  },
  methods: {
    async sendMsg() {
      if (this.loginReq.phone==null||this.loginReq.phone=='') {
        this.$message({
          message:'请输入手机',
          type:'error',
          offset:80
        })
        return;
      }else {
        console.log(this.loginReq.phone)
        // var params = {
        //   phone: this.loginReq.phone
        // }
        const res = await this.$axios.get('/api/login/sendSMS',{
          params:{
            phone: this.loginReq.phone
          }
        });
        console.log(res)
        if (this.canClick) return
        this.canClick = true
        this.content = this.totalTime + 's后重新发送'
        let clock = window.setInterval(() => {
          this.totalTime--
          this.content = this.totalTime + 's后重新发送'
          if (this.totalTime < 0) {
            window.clearInterval(clock)
            this.content = '重新发送短信'
            this.totalTime = 60
            this.canClick = false
          }
        }, 1000)
      }


    },
     submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // alert('submit!');
          const params = {
            phone: this.loginReq.phone,
            code: this.loginReq.code
          }
          this.$axios.post('/api/login/user',params).then((res)=>{
            if (res.data.success==true){
              this.$message({
                message:'登录成功',
                type:'success',
                offset:80
              })
              // this.$store.user("setUser", res.data.content);
              this.$store.commit("set_token", res.data.content.token);
              this.$store.commit("setUserInfo", res.data.content);

              setTimeout(() => {
                // 此时要判断/login后面的参数，若无参数，进入主页；
                this.$router.push("/index");
                location.reload()

                // 若有参数则参数为未有权限的那个路由，跳转到那个路由
                // this.$router.push(***); -- 具体要自己在这实现
              }, 100);
            }
          })

        } else {
          this.$message({
            message: '登录失败',
            type: 'error',
            offset: 80
          })
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>